/**
 * 
 */
package com.smartwork.common.mobile;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.smartwork.common.mobile.vo.BaseRequestData;
import org.restlet.data.Form;
import org.restlet.resource.ClientResource;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * 
 */
public abstract class BaseRestletClient {
	protected Form form;
	private String result;
	private String baseUrl;
	private String url;

	protected void init(String baseUrl) {
		this.baseUrl = baseUrl;
		form = new Form();
	}

	protected String post(String url) throws Exception {
		this.url = url;
		ClientResource client = new ClientResource(this.baseUrl + this.url);
		System.out.println("发送报文：" + form.getValuesMap());
		result = client.post(form).getText();
		return result;
	}

	protected void printPost(String url) throws Exception {
		post(url);
		print();
	}

	protected String get(String url) throws Exception {
		this.url = url;
		ClientResource client = new ClientResource(this.baseUrl + this.url);
		result = client.get().getText();
		System.out.println("发送报文：" + result);
		return result;
	}

	protected void printGet(String url) throws Exception {
		get(url);
		print();
	}

	protected void print() {
		System.out.println("连接：" + this.baseUrl + this.url);
		System.out.println("返回报文：" + result);
	}

	/**
	 * 
	 * 功能:数据转换
	 * 
	 * @param
	 * @date 2014-11-27
	 * @author wj
	 */
	public BaseRequestData getRequestData(Map<String, Object> dataMap) throws JsonProcessingException {
		Map<String, String> jsonMap = new HashMap<String, String>();
        ObjectMapper objectMapper=new ObjectMapper();
        if (dataMap != null) {
			for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
                jsonMap.put(entry.getKey(),objectMapper.writeValueAsString(entry.getValue()));
			}
		}
        BaseRequestData requestData = new BaseRequestData();
		return requestData;
	}
}
